"use strict";
/* Controllers */
app.controller("costTaskDetailController", ['$scope', '$location', '$rootScope', '$routeParams', 'costTaskDetailService', function ($scope, $location, $rootScope, $routeParams, costTaskDetailService) {

    var vm = $scope.vm;
    vm.result = {};
    vm.p = {};
    vm.p.ownerId = $routeParams.ownerId;
    vm.p.dt = $routeParams.dt;
    vm.p.taskId = $routeParams.taskId;
    vm.p.type = 'task';


    if(typeof(vm.p.ownerId)=='undefined' || vm.p.ownerId=='0') {
        vm.p.ownerId = null;
    }
    if(typeof(vm.p.dt)=='undefined' || vm.p.dt=='0') {
        vm.p.dt = getNowFormatDate(-1);
    }

    var errorFn = function (result) {
        alert(result);
    };



    $scope.listCostTask = function () {
        var vm = $scope.vm;
        var params = {};
        params = vm.p;
        $('#list_table').bootstrapTable('showLoading');
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;

            $('#list_table').bootstrapTable('load', data);
            $('#list_table').bootstrapTable('hideLoading');
        };
        vm.result.ms = '...';
        costTaskDetailService.listCostTask(params, procFn, errorFn);
    };

    $scope.clearSearch = function () {
        var vm = $scope.vm;
        vm.p.searchType = '';
        vm.p.endpoint = '';
        vm.p.areaName = '';
    };


    $scope.searchCostTask = function () {
        $('#list_table').bootstrapTable('refresh');
        $scope.listCostTask()
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 100, 1000], pageSize: 30
        });

        $resultTable.bootstrapTable('hideColumn', 'appName');
        $resultTable.bootstrapTable('hideColumn', 'depLevel');
        $resultTable.bootstrapTable('hideColumn', 'priority');
        $resultTable.bootstrapTable('hideColumn', 'hadoopQueueName');

        $resultTable.bootstrapTable('hideColumn', 'basicTime');
        $resultTable.bootstrapTable('hideColumn', 'taskUseCores');
        $resultTable.bootstrapTable('hideColumn', 'taskMaxUseMem');
        $resultTable.bootstrapTable('hideColumn', 'taskMemUseRate');

        $scope.listCostTask();
    };

    $scope.initializeController();
}]);




/* Services */
app.service('costTaskDetailService', ['ajaxService', function (ajaxService) {

    this.listCostTask = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/cost_report/listCostTaskReportDetail.do", successFunction, errorFunction);
    };

}]);

function costTaskDetailFmt(value, row, index) {
    //var optCont1 =
    //'<a type="button" class="btn btn-xs btn-primary" href="#/star/cost/cost_task_config/' + row.taskId + '" target="_blank">基线配置</a>';
    var optCont2 =
    '<a type="button" class="btn btn-xs btn-primary" href="#/star/order/cost_task_detail_chart/'+ row.taskId + '" target="_blank">运行分析</a>';
    return [
        '<div style="width:9rem">',
        //optCont1,
        optCont2,
        '</div>'
    ].join('');
}
function time2HourFormat(value, row, index) {
    if(value==null) return '-';
    return value.substr(11,8);
}

function taskIdFormat(value, row, index) {
    if(value==null) return '-';
    return '<a href="https://dp.58corp.com/data-develop/task-list/task-detail/' + value + '" target="_blank"><u>'+value+'</u></a>';
}


function taskUseTimeFormat(value, row, index) {
    var secondVal = value
    if (secondVal < 1) {
        return '-'
    }
    if (secondVal < 60) {
        return secondVal.toFixed(0) + "s"
    } else if (secondVal >= 60 && secondVal < 3600) {
        return (secondVal / 60).toFixed(2) + "min"
    } else if (secondVal >= 3600) {
        return (secondVal / 60 / 60).toFixed(2) + "h"
    }
}

function taskAlcCores2CostFormat(value, row, index) {
    if(value==null) return '-';
    // useTime 是到单位秒的
    var cost =  (value*(row.useTime/60/60/24)/30*150).toFixed(2);
    return cost;
}
function rateFormat(value, row, index) {
    if(value==null) return '-';
    return (value*100).toFixed(2) + '%';
}


